﻿using System;

using DreamCube.Foundation.Serialization;

namespace DreamCube.Framework.Utilities.Report
{
    public static class ElementHelper
    {
        /// <summary>
        /// 根据Element对象的配置信息创建配置的Element
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="el"></param>
        /// <returns></returns>
        public static T Create<T>(Element el)
            where T : Element
        {
            T t = MyJson.Deserialize<T>(el.Config);
            return t;
        }

        /// <summary>
        /// 根据Element对象的配置信息创建一个Element对象
        /// </summary>
        /// <param name="el"></param>
        /// <returns></returns>
        public static Element Create(Element el)
        {
            if (el.ElementType == Enums.ElementType.Report)
                return MyJson.Deserialize<ReportForm>(el.Config);

            if (el.ElementType == Enums.ElementType.Table)
                return MyJson.Deserialize<Table>(el.Config);

            if (el.ElementType == Enums.ElementType.Br)
                return MyJson.Deserialize<Br>(el.Config);

            if (el.ElementType == Enums.ElementType.TableDataTD)
                return MyJson.Deserialize<TableDataTD>(el.Config);

            if (el.ElementType == Enums.ElementType.TableDataTR)
                return MyJson.Deserialize<TableDataTR>(el.Config);

            if (el.ElementType == Enums.ElementType.TableTitleTD)
                return MyJson.Deserialize<TableTitleTD>(el.Config);

            if (el.ElementType == Enums.ElementType.TableTitleTR)
                return MyJson.Deserialize<TableTitleTR>(el.Config);

            if (el.ElementType == Enums.ElementType.SearchCondition)
                return MyJson.Deserialize<SearchCondition>(el.Config);
            return null;
        }
    }
}
